<?php

namespace app\admin\controller;

use app\model\AdminInfo as ModelAdminInfo;
use app\admin\logic\Admin as AdminLogicAdmin;
use app\model\SysSetting as ModelSysSetting;

class Admin extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加管理员账号信息 \n
     * URI : /admin/add.account.info
     * @param :
     *  name          | type   | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填 | 用户票据
     *  groupId       | int    | 必填 | 角色编号
     *  adminName     | string | 必填 | 管理员名称
     *  mobile        | string | 必填 | 联系电话(手机号码)
     *  department    | string | 必填 | 部门
     *  loginAccount  | string | 必填 | 登录账号（必须唯一）
     *  loginPassword | string | 必填 | 登录密码（大于等于6位的的字符串即可）
     *
     * @return
     *  name  | type  | description
     * -------|-------|-------------
     *  ----- | ----  | 无
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addAdminAccountInfo()
    {
        $str_admin_name = $this->_getContentParams('adminName');
        _toCompare(empty($str_admin_name), '===', true, 12073);

        $int_group_id = (int)$this->_getContentParams('groupId');
        _toValidate('id', $int_group_id, 12057);

        $str_mobile = $this->_getContentParams('mobile');
        _toValidate('mobile', $str_mobile, 12055);

        $str_department = $this->_getContentParams('department');
        _toCompare(empty($str_department), '===', true, 12056);

        $str_login_account = $this->_getContentParams('loginAccount');
        _toValidate('account', $str_login_account, 12074);

        $str_login_password = $this->_getContentParams('loginPassword');
        _toValidate('login_pwd', $str_login_password, 12076);

        $int_return_id = ModelAdminInfo::getInstance()->checkIsSetAccount($str_login_account);
        _toCompare($int_return_id, '>', 0, 12075);

        $int_admin_id = AdminLogicAdmin::getInstance()->createAdminInfo($str_admin_name, $str_mobile, $str_department, $str_login_account, $str_login_password, $int_group_id, 0);

        _returnData(['adminId' => $int_admin_id]);
    }

    /**
     * 获取管理员账户信息列表 \n
     * URI : /admin/get.account.list
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     *  passport   | string | 必填  | 用户票据
     *  page       | string | 必填  | 当前页码
     *  listRows   | string | 必填  | 一页的条数
     *
     * @return
     *  name       | type  | description
     * ------------|-------|-------------
     *  totalPages | int   | 总页数
     *  list       | array | 列表
     *
     * list:
     *  name         | type   | description
     * --------------|--------|-------------
     *  adminId      | int    | 管理员账户编号
     *  isSuper      | int    | 是否是超级管理员 0-不是 1-是
     *  adminName    | string | 姓名管理员
     *  mobile       | string | 联系电话
     *  department   | string | 部门
     *  roleName     | string | 角色
     *  loginAccount | string | 登录账号
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAccountList()
    {
        $int_now_page = $this->_getContentParams('page'); // 当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows'); // 一页的条数
        _toValidate('id', $int_list_rows, 10006);

        $arr_where = [];

        $int_total_rows = ModelAdminInfo::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

        $arr_list = (array)ModelAdminInfo::getInstance()->getAccountList($arr_where, 'fa.id AS adminId,fa.is_super AS isSuper,fa.admin_name AS adminName,fa.mobile,fa.department,fg.role_name AS roleName,fa.login_account AS loginAccount', $int_first_row, $int_list_rows, 'fa.id DESC');

        _returnData(['totalPages' => $int_total_pages, 'list' => $arr_list]);
    }

    /**
     * 获取管理员账号详细信息 \n
     * URI : /admin/get.account.info
     * @param :
     *  name      | type   | null | description
     * -----------|--------|------|-------------
     *  passport  | string | 必填  | 用户票据
     *  adminId   | int    | 必填  | 管理员编号
     *
     * @return
     *  name         | type   | description
     * --------------|--------|-------------
     *  adminId      | int    | 管理员编号
     *  adminName    | string | 管理员姓名
     *  mobile       | string | 联系电话
     *  department   | string | 部门
     *  isSuper      | int    | 是否是超级管理员 1-是
     *  groupId      | int    | 角色编号 当isSuper=1时 groupId=0
     *  loginAccount | string | 登录账号
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAccountInfo()
    {
        $int_admin_id = (int)$this->_getContentParams('adminId');
        _toValidate('id', $int_admin_id, 12101);

        $arr_where = [
            'id' => $int_admin_id,
        ];

        $arr_info = ModelAdminInfo::getInstance()->toFind($arr_where, 'id AS adminId,admin_name AS adminName,mobile,department,is_super AS isSuper,group_id AS groupId,login_account');

        _returnData($arr_info);
    }

    /**
     * 删除管理员账号信息 \n
     * URI : /admin/delete.account.info
     * @param :
     *  name      | type   | null | description
     * -----------|--------|------|-------------
     *  passport  | string | 必填  | 用户票据
     *  adminId   | int    | 必填  | 管理员编号
     *
     * @return
     *  name   | type | description
     * --------|------|-------------
     *  ------ | ---- | 无
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function deleteAccountInfo()
    {
        $int_admin_id = (int)$this->_getContentParams('adminId');
        _toValidate('id', $int_admin_id, 12101);

        $arr_where = [
            'id' => $int_admin_id,
            'is_super' => 0,
        ];

        $arr_info = ModelAdminInfo::getInstance()->toFind($arr_where, 'id');

        _toCompare((isset($arr_info['id'])), '===', false, 12059);

        _toCompare($arr_info['id'], '==', $this->admin_id, 12060);

        ModelAdminInfo::getInstance()->toDelete($int_admin_id);

        _returnData();
    }
    /*
     * 系统配置
     * URL：/admin.system.config
     */
    public function systemConfig()
    {
        $page = $this->_getContentParams('page');
        if (empty($page)) $page = 1;
        $listRow = $this->_getContentParams('listRows');
        _toCompare(empty($listRow),'==',true,10006);
        $obj = new \app\logic\Page();
        $data = $obj->getList(ModelSysSetting::getInstance(),[],'',$page,$listRow,'','configList');
        _returnData($data);
    }

    /**
     * 系统配置-》值修改
     * url：/admin.system.configValueModify
     */
    public function systemConfigValueModify(){
        $id = $this->_getContentParams('id');
        _toValidate('id',$id,20009);
        $value = $this->_getContentParams('value');
        _toCompare(empty($value),'==',true,20011);
        $arr_where['value']=$value;
        $data = ModelSysSetting::getInstance()->toUpdate("id=$id",$arr_where);
        if ($data>0){
            _returnData();
        }
    }

    /**
     * 系统配置-》描述修改
     * url：/admin.system.configDescribeModify
     */
    public function systemConfigDescribeModify(){
        $id = $this->_getContentParams('id');
        _toValidate('id',$id,20009);
        $describe = $this->_getContentParams('describe');
        _toCompare(empty($describe),'==',true,20010);
        $arr_where['describe']=$describe;
        $data = ModelSysSetting::getInstance()->toUpdate("id=$id",$arr_where);
        if ($data>0){
            _returnData();
        }
    }
}
